<?php

//tailvh@gmail.com

//Require model files
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Category.php';

//Require objects files
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Category.php';

//Require forms files
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Category.php';




class CategoryController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private  $modelCategory;

    private $_redirector;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////

    public function preDispatch() {

    }

    public function init() {
        $layoutPath = APPLICATION_PATH . '/templates/default';
        $option = array('layout' => 'index',
        'layoutPath' => $layoutPath);
        
        Zend_Layout::startMvc($option);
        $this->modelCategory = new End_Models_Category(); 
        $this->_redirector = $this->_helper->getHelper('Redirector');
   }

    ///////////////////////
    // U T I L I T I E S //
    ///////////////////////
   
        
    ///////////////////
    // A C T I O N S //
    ///////////////////
    //INDEX ACTION
    public function indexAction() { 
        
        $tmpArrCategory = $this->modelCategory->fetchAll();
        $this->view->arrCategory = $tmpArrCategory; 
    }
    
    public function addAction(){ 
        $formCategory = new Application_Form_Category();
        $arrParent = $this->modelCategory->_list();
        $formCategory->parent_id->addMultiOptions($arrParent);   
        
        //var_dump($var); - xuất giá trị của $var.
        if ($this->getRequest()->isGet()) { 
          
        } else {
            $Cancel = $this->getRequest()->getPost('cancel', '0');
            if ($Cancel) {
                $this->_redirector->gotoUrl('category/index');
            }
            
            $formData = $this->getRequest()->getPost();
            if ($formCategory->isValid($formData)) { 
                $formCategory->category_image->receive();
                $toDate = date('Y-m-d');
                     
                $category = new Category(null, $formCategory->category_image->getFileName(null, 0),  $formData['category_name'], $formData['parent_id'], $formData['sort_order'], 
                                 $toDate, $toDate);
               
                $id = $this->modelCategory->_insert($category);

                $this->_redirector->gotoUrl('category/index');
            } else { 
                $formProduct->populate($formData);
            }
        }
        //die();
        $this->view->form = $formCategory;
    }
    
    public function deleteAction(){
        $id = $this->_getParam('id','0');
        $Category = $this->modelCategory->_find($id);
        if(isset($Category))
        {
            $this->modelCategory->_delete($id);
        }
        
        $this->_redirector->gotoUrl('category/index'); 
    }
    
    public function editAction(){
        $id = $this->_getParam('id','0');
        $Category = $this->modelCategory->_find($id);
        $formCategory = new Application_Form_Category();
        $arrParent = $this->modelCategory->_list();
        $formCategory->parent_id->addMultiOptions($arrParent);   
        
        if (!$this->getRequest()->isGet())  
        {
            if(isset($Category))
            {               
                $Cancel = $this->getRequest()->getPost('cancel', '0');
                if ($Cancel) {
                    $this->_redirector->gotoUrl('Category/index');
                }

                $formData = $this->getRequest()->getPost();
                if ($formCategory->isValid($formData)) { 
                    $toDate = date('Y-m-d'); 
                    $formCategory->category_image->receive();
                    
                    $Category->category_id              = $formData['category_id'];
                    $Category->category_image           = $formCategory->category_image->getFileName(null, 0); 
                    $Category->category_name            = $formData['category_name'];
                    $Category->parent_id                = $formData['parent_id'];
                    $Category->sort_order               = $formData['sort_order'];
                    $Category->last_modified            = $toDate;
                    
                    $id = $this->modelCategory->_update($Category); 
                            
                    $this->_redirector->gotoUrl('Category/index');
                } else { 
                    $formCategory->populate($formData);
                }
            }else
            {
                $this->_redirector->gotoUrl('Category/index');
            }    
            $this->view->form = $formCategory;
        }    
        else                                                                    //lần đầu gọi tới ta sẽ gán giá trị của đối tượng lên form
        {       
            if(isset($Category))                                                 //nếu đối tượng tồn tại
            { 
                $UC = new Utility_Converter();                                  //tạo công cụ giúp đổi đối tượng thành mảng
                $formCategory->populate($UC->objectToArray($Category));           //đổi đối tượng thành mảng rồi gán cho form vừa tạo
                $this->view->form = $formCategory;
            }
            else
            {
                $this->_redirector->gotoUrl('Category/index');   
            }
        }
    }
    
}